var students = [
    {id:202590223001,name:"周杰伦",gender:true,state:0},
    {id:202590223002,name:"刘德华",gender:true,state:1},
    {id:202590223003,name:"张曼玉",gender:false,state:2},
    {id:202590223004,name:"郭富城",gender:true,state:3},
    {id:202590223005,name:"刘亦菲",gender:false,state:4}
];

//创建一个新用户
function add(){
    var code = document.getElementById("code").value;
    var sex = -1;
    Array.from(document.getElementsByName("gender")).forEach(element => {
      if (element.checked) {
        sex = element.value;
        return;
      }
    });
    var name = document.getElementById("name").value;
    var state = document.getElementById('state').value;
    var nodeHtml = `
       <td><input type="checkbox" class="input-control input-check"></td>
       <td>${code}</td>
       <td>${name}</td>
       <td>${sex = 0 ? '男' : '女'}</td>
       <td>${state}</td>
       <td>
         <button type="button" class="btn btn-info" onclick="edit(this)">编辑</button>
         <button type="button" class="btn btn-danger" onclick="del(this)">删除</button>
       </td>
    `;
    var tr = document.createElement("tr");
    tr.innerHTML = nodeHtml;
    document.getElementById("tableData").appendChild(tr)
}

//编辑

function edit(dom){
    var doms = Array.from(dom.parentNode.parentNode.children);
    var code = doms[1].innerText;
    var name = doms[2].innerText;
    var sex = doms[3].innerText == "男" ? 0 : 1;
    var state = doms[4].innerText;
    document.getElementById("code").value = code;
    var gender = Array.from(document.getElementsByName("gender"));
    gender.forEach(element => {
      if (element.value == sex) {
        element.checked = "checked";
      }
    });
    document.getElementById("name").value = name;
    document.getElementById("state").value = state;

    document.getElementById("save").style.display = "inline-block";
    document.getElementById("create").style.display = "none";
    document.getElementById("table").style.display = "none";
    editTr = dom.parentNode.parentNode;
  }
  var editTr = null;
  //保存
  function save() {
    var code = document.getElementById("code").value;
    var sex = -1;
    Array.from(document.getElementsByName("gender")).forEach(element => {
      if (element.checked) {
        sex = element.value;
        return;
      }
    });
    var name = document.getElementById("name").value;
    var state = document.getElementById('state').value;

    var doms = Array.from(editTr.children);
    doms[1].innerText = code;
    doms[2].innerText = sex == 0 ? '男' : '女';
    doms[3].innerText = name;
    doms[4].innerText = state;
    document.getElementById("save").style.display = "none";
    document.getElementById("create").style.display = "inline-block";
    document.getElementById("table").style.display = "table";
}

//单个删除
function delOne(obj){
    var delFlag = window.confirm('确认要删除吗？');
    if (delFlag) {
        var delTr = obj.parentElement.parentElement;
        var oTbody = document.getElementById("tableData");
        oTbody.removeChild(delTr);
    }
}
//全选
function checkAll(obj) {
    var isChecked = obj.checked;
    var items = document.getElementsByName("ids");
    items.forEach((items, index) => {
        items.checked = isChecked;
    })
}
//全选2
function uncheckAll(obj){
    var isChecked = obj.checked;
    console.log(isChecked);
    if(!isChecked){
        var checkAll = document.getElementById("checkAll");
       checkAll.checked = false;
    }
}

//批量删除
function delAll() {
    var oTbody = document.getElementById("tableData");
    var items = document.getElementsByName("ids");
    var count = 0;
    var delitems = [];
    for (var item of items) {
        if(item.checked){
            count++;
        delitems.push(item);
        }      
    }
    if (count > 0) {
        var flag = confirm("确定删除?");
        if(flag){
            for(var i = 0;i<delitems.length;i++){
                var delitem = delitems[i];
                var delTr = delitem.parentElement.parentElement;
                oTbody.removeChild(delTr);
            }
            checkAll.checked = false;
        }
    } else {
        alert("未选定任何记录");
    }
}

